﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using SqlSugar;
using System.Drawing.Printing;

namespace coreapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "v4")]
    public class RoleController : ControllerBase
    {

        private readonly ISqlSugarClient db;
        public RoleController(ISqlSugarClient _db)
        {
            this.db = _db;
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GteRole")]
        public IActionResult GetRole()
        {
            //List<EmployeeRole> list = null;
              var rolelist = db.Queryable<EmployeeRole>().ToList();
            //var newlist = db.Queryable<EmployeeRole>().Where(it => list.Any(s => s.RoleID == it.RoleID && s.RoleName == it.RoleName));
            return Ok(rolelist);
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GteRoleList")]
        public IActionResult GetRolelist(string? name,int pagenumber,int pageSize)
        {
            int totalCount = 0;
            var page = db.Queryable<EmployeeRole>();
            if (!string.IsNullOrEmpty(name))
            {
                //模糊查询
                page.Where(a => a.RoleName.Contains(name));
            }

            var data = new
            {
                data = page.ToPageList(pagenumber, pageSize, ref totalCount),
                totalCount = totalCount
                
            };
            return Ok(data);
        }

        /// <summary>
        /// 添加职位信息
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddRole")]
        public IActionResult PostRole(EmployeeRole role)
        {


            //返回插入行数
            int i = db.Insertable(role).ExecuteCommand();
            return Ok(i > 0 ? true : false);
        }

        /// <summary>
        /// 修改职位信息
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult PutRole(EmployeeRole role)
        {
            var result = db.Updateable(role).ExecuteCommand();//实体有多少列更新多少列
            return Ok(result > 0 ? true : false);
        }

        /// <summary>
        /// 删除职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteRole(int id)
        {
           int i= db.Deleteable<EmployeeRole>().In(id).ExecuteCommand();
            return Ok(i>0?true:false);
        }

        /// <summary>
        /// 获取薪资
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSalary")]
        public IActionResult GetSalary()
        {
            //List<EmployeeRole> list = null;
            var slist = db.Queryable<EmployeeSalary>().ToList();
            //var newlist = db.Queryable<EmployeeRole>().Where(it => list.Any(s => s.RoleID == it.RoleID && s.RoleName == it.RoleName));
            return Ok(slist);
        }

        /// <summary>
        /// 获取薪资列表,分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSalarylist( int pagenumber, int pageSize)
        {
            int totalCount = 0;
            var page = db.Queryable<EmployeeSalary>();

            var data = new
            {
                data = page.ToPageList(pagenumber, pageSize, ref totalCount),
                totalCount = totalCount

            };
            return Ok(data);
        }


        /// <summary>
        /// 添加薪资
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddSalary")]
        public IActionResult PostSalary(EmployeeSalary Salary)
        {

            // 定义存储过程参数和返回值
            var parameters = new SugarParameter[]
            {
                new SugarParameter("@BasicSalary", Salary.BasicSalary), // 基本工资参数
                new SugarParameter("@Bonus", Salary.Bonus), // 奖金参数
                new SugarParameter("@TaxRate", Salary.TaxRate), // 税率参数
                new SugarParameter("@Result", true, true) // 输出参数
            };
            // 调用存储过程
            var result = db.Ado.SqlQuery<int>("EXEC Proc_AddEmpSalary @BasicSalary, @Bonus, @TaxRate, @Result OUTPUT", parameters);

            // 获取输出参数的值
            var outputResult = Convert.ToInt32(parameters[3].Value);

            return Ok(outputResult==1?true:false); 

        }

        /// <summary>
        /// 修改薪资
        /// </summary>
        /// <param name="salary"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("PUTSalary")]
        public IActionResult PutSalary(EmployeeSalary Salary)
        {
            // 定义存储过程参数和返回值
            var parameters = new SugarParameter[]
            {   new SugarParameter("@SalaryID",Salary.SalaryID),
                new SugarParameter("@BasicSalary", Salary.BasicSalary), // 基本工资参数
                new SugarParameter("@Bonus", Salary.Bonus), // 奖金参数
                new SugarParameter("@TaxRate", Salary.TaxRate), // 税率参数
                new SugarParameter("@Result", true, true) // 输出参数
            };
            // 调用存储过程
            var result = db.Ado.SqlQuery<int>("EXEC Proc_EditEmpSalary @SalaryID ,@BasicSalary, @Bonus, @TaxRate, @Result OUTPUT", parameters);

            // 获取输出参数的值
            var outputResult = Convert.ToInt32(parameters[4].Value);

            return Ok(outputResult == 1 ? true : false);

        }

        /// <summary>
        /// 删除薪资信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DelSalary")]
        public IActionResult DeleteSalary(int id)
        {

            int i = db.Deleteable<EmployeeRole>().In(id).ExecuteCommand();
            return Ok(i > 0 ? true : false);
        }






    }
}
